package com.lin.util;

import com.lin.base.Role;
import com.lin.entity.SecurityUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;


public class SecurityUtil {
    public static Integer getUid() {
        try {
            return ((SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUid();
        } catch (Exception e) {
            return 0;
        }
    }

    public static List<GrantedAuthority> getRole() {
        return ((SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAuthorities();
    }

    public static Boolean hasRole(Role role) {
        List<GrantedAuthority> authorities = getRole();
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals(role.role))
                return true;
        }
        return false;
    }
}
